package presentation.playgameForm_UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import business.playgameForm_Evt.GameMode_Evt;

public class NextLevel_UI extends JDialog {

	private static final long	serialVersionUID	= 1L;
	private JPanel				panelWordFound, panelListWord, panelButton, panelMain;
	public JLabel				lbWordFound, lbScore;
	private JScrollPane			scroll;
	public JTextArea			taListWord;
	private JButton				btNextLevel, btSaveGame;
	private GridBagConstraints	c;

	private GameMode_Evt		event				= new GameMode_Evt();

	private int					gameMode_point		= 0;
	private int					gameMode_score		= 0;
	private int					gameMode_target		= 50;
	private int					gameMode_time		= 120;
	private int					gameMode_level		= 0;
	private String				gameMode_textTemp	= "";

	public boolean				check				= false;
	
	public int gameMode;

	public NextLevel_UI(JFrame owner, int gameMode) {
		super(owner, true);
		this.gameMode = gameMode;
		initComponents();
		buttonEvent();
		keyEvent();
		mouseEvent();

		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.setTitle("Message");
		this.setSize(this.getSize());
		this.setLocationRelativeTo(owner);
		btNextLevel.requestFocus();
	}

	private void initComponents() {
		setPanelWordFound();
		setPanelListWord();
		setPanelButton();

		panelMain = new JPanel(new GridBagLayout());
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(10, 10, 10, 10);
		c.anchor = GridBagConstraints.NORTH;
		panelMain.add(panelWordFound, c);

		c.gridy = 1;
		c.anchor = GridBagConstraints.CENTER;
		panelMain.add(scroll, c);

		c.gridy = 2;
		c.anchor = GridBagConstraints.SOUTH;
		c.insets = new Insets(5, 20, 10, -150);
		panelMain.add(panelButton, c);

		this.add(panelMain);
		this.pack();

	}

	private void setPanelWordFound() {
		panelWordFound = new JPanel(new BorderLayout());
		lbWordFound = new JLabel("Word Found: 0");
		lbWordFound.setFont(new Font("Comic Sans MS", 0, 20));
		panelWordFound.add(lbWordFound, BorderLayout.WEST);

		lbScore = new JLabel("Score: 0");
		lbScore.setFont(new Font("Comic Sans MS", 0, 20));
		panelWordFound.add(lbScore, BorderLayout.EAST);

	}

	private void setPanelListWord() {

		panelListWord = new JPanel();
		scroll = new JScrollPane();
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setPreferredSize(new Dimension(400, 100));
		taListWord = new JTextArea();
		scroll.setViewportView(taListWord);
		panelListWord.add(scroll);
		taListWord.setFont(new Font("Comic Sans MS", 0, 16));
		taListWord.setEditable(false);
		taListWord.setBackground(new Color(201, 228, 255));
	}

	private void setPanelButton() {
		btNextLevel = new JButton("Next Level");
		btNextLevel.setFont(new Font("Comic Sans MS", 0, 14));
		btSaveGame = new JButton("Save Game");
		btSaveGame.setFont(new Font("Comic Sans MS", 0, 14));
		panelButton = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
		panelButton.add(btNextLevel);
		panelButton.add(btSaveGame);
		this.add(panelButton);
		
		if(gameMode == GameMode_UI.PRACTICE_MODE)
		{
			btSaveGame.setEnabled(false);
		}
	}

	private void buttonEvent() {
		btNextLevel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		btSaveGame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				saveGame();
				JOptionPane.showMessageDialog(null, "Game saved!", "Message", JOptionPane.INFORMATION_MESSAGE);
				setVisible(false);
			}
		});
	}

	private void keyEvent() {
		btNextLevel.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT) {
					btSaveGame.requestFocus();
				}
			}
		});

		btSaveGame.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent evt) {
				if (evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT) {
					btNextLevel.requestFocus();
				}
			}
		});
	}

	private void mouseEvent() {
		taListWord.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent evt) {
				btNextLevel.requestFocus();
			}
		});
	}

	public void setValue(int gameMode_level, int gameMode_score, int gameMode_target)
	{
		this.gameMode_level = gameMode_level + 1;
		this.gameMode_score = gameMode_score;
		this.gameMode_target = gameMode_target + 20;
		this.gameMode_point = 0;
		this.gameMode_time = 120;
		this.gameMode_textTemp = event.getAvailabelWord();
	}
	
	private void saveGame()
	{
		event.saveGame(this.gameMode_level, this.gameMode_score, this.gameMode_target, this.gameMode_point, this.gameMode_time, this.gameMode_textTemp);
	}
}
